from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from pyceab.course.models import Course 
from pyceab.plan.models import PlannedCourse
from pyceab.schedule.models import Offering, TimeSlot
from django.http import HttpResponse
from pyceab.utils import get_plan_for_current_user
# Create your views here.

DAY_CHOICES = {
        'M': 'monday',
        'T': 'tuesday',
        'W': 'wednesday',
        'Th': 'thursday',
        'F': 'friday',
        }


def course_schedule(request, course_id):
    """
    Each timeslot is rendered in a weekly calendar. Required
    properties are:
        top: integer value of css top property. Indicates 
        beginning of the timeslot, use the top property of the
        timeslot.
        height: css height in pixels, corresponds to length of
        timeslot, get from slot.height.
        type: LEC, TUT, LAB, from offering.type
        title: text to display in the timeslot
        day: monday, tuesday, etc... eventually sets class on 
        the timeslot div to position the bubble in the correct day
        identifier: switch colours based on this identifier. In a course's
        case, switch based on the offering_id of the timeslot's offering.
        timeslots MUST be ordered by identifier before being passed to
        the template
    """
    # course = get_object_or_404(Course, id=course_id)
    course_offerings = Offering.objects.filter(course__id=course_id)
    timeslots = []
    for offering in course_offerings:
        timeslots +=[{
            'top': slot.top, 
            'height': slot.height, 
            'type': offering.type,
            'title': offering.course.short_form + ' - ' + offering.type,
            'day': DAY_CHOICES[slot.day],
            'identifier': offering.id,
            } for slot in offering.timeslot_set.all()]

    days = [ day_name for day_name in DAY_CHOICES.values()]
    minor_ticks = range(0, 405, 30)
    major_ticks = range(15, 405, 30)
    hours = zip(range(9,22), range(15, 405, 30))
    context = {
                'timeslots': timeslots,
                'day_names': days,
                'minor': minor_ticks,
                'major': major_ticks,
                'hours': hours,
            }
    return render_to_response('schedule/visualization.html',
            context,
            context_instance=RequestContext(request))

def term_schedule(request, term_id):
    """
    Each timeslot is rendered in a weekly calendar. Required
    properties are:
        top: integer value of css top property. Indicates 
        beginning of the timeslot, use the top property of the
        timeslot.
        height: css height in pixels, corresponds to length of
        timeslot, get from slot.height.
        type: LEC, TUT, LAB, from offering.type
        title: text to display in the timeslot
        day: monday, tuesday, etc... eventually sets class on 
        the timeslot div to position the bubble in the correct day
        identifier: switch colours based on this identifier. In a term's
        case, switch based on the course_id of the offering's course.
        timeslots MUST be ordered by identifier before being passed to
        the template
    """
    student = request.user.student_set.get()
    term_code = student.get_term_code(term_id)
    term_offerings = student.schedule.offerings.filter(term=term_code).order_by('course')
    timeslots = []
    for offering in term_offerings:
        timeslots +=[{
            'top': slot.top, 
            'height': slot.height, 
            'type': offering.type,
            'title': offering.course.short_form + ' - ' + offering.type,
            'day': DAY_CHOICES[slot.day],
            'identifier': offering.course.id,
            } for slot in offering.timeslot_set.all()]

    days = [ day_name for day_name in DAY_CHOICES.values()]
    minor_ticks = range(0, 405, 30)
    major_ticks = range(15, 405, 30)
    hours = zip(range(9,22), range(15, 405, 30))
    context = {
                'timeslots': timeslots,
                'minor': minor_ticks,
                'major': major_ticks,
                'day_names': days,
                'hours': hours,
            }
    return render_to_response('schedule/visualization.html',
            context,
            context_instance=RequestContext(request))
